package com.unittec.drainage.provider.utils;

import com.unittec.drainage.provider.ApplicationContextProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.env.Environment;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author wuxinwei
 */
public class OssUtils {
    private static final String OSS_TYPE_ALIYUN = "aliyun";
    private static final String OSS_TYPE_ECLOUD = "ecloud";
    private static String OSS_TYPE = "";

    static {
        if (StringUtils.isBlank(OSS_TYPE)) {
            Environment environment = ApplicationContextProvider.getApplicationContext().getBean(Environment.class);
            String ossType = environment.getProperty("oss.type");
            OSS_TYPE = StringUtils.isBlank(ossType) ? OSS_TYPE_ALIYUN : ossType;
        }

    }


    /**
     * 下载文件
     *
     * @param path
     * @param target
     * @throws IOException
     */
    public static void download(String path, File target) throws IOException {
        if (OSS_TYPE_ALIYUN.equalsIgnoreCase(OSS_TYPE)) {
            OssAliYunUtils.download(path, target);
        } else if (OSS_TYPE_ECLOUD.equalsIgnoreCase(OSS_TYPE)) {
            OssEcloudUtils.download(path, target);
        }
    }

    /**
     * 上传文件
     *
     * @param file
     * @param basePath
     * @param fileName
     * @return
     */
    public static String upload(File file, String basePath, String fileName) {
        String path = "";
        if (OSS_TYPE_ALIYUN.equalsIgnoreCase(OSS_TYPE)) {
            path = OssAliYunUtils.upload(file, basePath, fileName);
        } else if (OSS_TYPE_ECLOUD.equalsIgnoreCase(OSS_TYPE)) {
            path = OssEcloudUtils.upload(file, basePath, fileName);
        }
        return path;
    }

    /**
     * 上传文件到OSS
     *
     * @param path
     * @param fileName
     * @param is
     * @return
     */
    public static String uploadObject2Oss(String path, String fileName, InputStream is) throws Exception {
        String url = "";
        if (OSS_TYPE_ALIYUN.equalsIgnoreCase(OSS_TYPE)) {
            url = OssAliYunUtils.uploadObject2Oss(path, fileName, is);
        } else if (OSS_TYPE_ECLOUD.equalsIgnoreCase(OSS_TYPE)) {
            url = OssEcloudUtils.uploadObject2Oss(path, fileName, is);
        }
        return url;
    }

    /**
     * 获得url链接
     *
     * @param key
     * @return
     */
    public static String getUrl(String key, int day) {
        String url = "";
        if (OSS_TYPE_ALIYUN.equalsIgnoreCase(OSS_TYPE)) {
            url = OssAliYunUtils.getUrl(key, day);
        } else if (OSS_TYPE_ECLOUD.equalsIgnoreCase(OSS_TYPE)) {
            url = OssEcloudUtils.getUrl(key, day);
        }
        return url;
    }

    /**
     * 异步上传文件到OSS
     *
     * @param path
     * @param fileName
     * @param is
     * @return
     */
    public static String uploadObject2OssAnsy(String path, String fileName, InputStream is) throws Exception {

        String url = null;
        try {
            url = OssAliYunUtils.uploadObject2OssAnsy(path, fileName, is);
        } catch (Exception e) {
            throw new Exception("upload file error");
        }
        return url;
    }


}
